//
//  LLKCollectionManage.m
//
//
//  Created by QianFeng on 16/5/9.
//  Copyright © 2016年 李龙坤. All rights reserved.
//

#import "LLKCollectionManage.h"
//#import "FMDatabase.h"
@interface LLKCollectionManage ()
//数据库
@property (nonatomic,strong)FMDatabase *dataBase;
//数据库路径
@property (nonatomic,copy)NSString * dbPath;
@end
@implementation LLKCollectionManage
//单例
+ (instancetype)shareManager {
    static LLKCollectionManage * mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mgr = [[LLKCollectionManage alloc] init];
    });
    return mgr;
}
- (instancetype)init {
    self = [super init];
    if (self) {
        //创建数据库
        _dataBase = [FMDatabase databaseWithPath:self.dbPath];
        //如果能打开数据库
        if ([_dataBase open]) {
            NSString *sql = @"create table if not exists appdetail(videoId text,videoname text,videoportraitUri text,videoisRecommend text,prisenumber text,collectnumber text,videourl text)";
            if ([_dataBase executeUpdate:sql]) {
                NSLog(@"成功");
                
            } else {
                NSLog(@"失败");
            }
            //关闭数据，
            [_dataBase close];
        }
    }
    return self;
}

- (NSString *)dbPath {
    if (_dbPath == nil) {
        //获取沙盒路径
        NSString * path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];
        _dbPath = [path stringByAppendingPathComponent:@"long2.sqlite"];
    }
    return _dbPath;
}

//添加收藏
- (BOOL)addCollect:(LKVideoModel *)video{
    
    BOOL success = false;
    
   NSString *sql = @"insert into appdetail(videoId,videoname,videoportraitUri ,videoisRecommend,prisenumber,collectnumber,videourl) values(?,?,?,?,?,?,?)";
    if ([_dataBase open]) {
        if ([_dataBase executeUpdate:sql,video.videoId,video.videoName,video.coverImage,video.isRecommend,video.praiseCount,video.collectionCount,video.videoUrl]) {
            success = YES;
            NSLog(@"收藏成功");
        } else {
            success = NO;
            NSLog(@"收藏失败");
        }
        [_dataBase close];
    }
    
    return success;
}


- (NSMutableArray *)selectAll {
    NSMutableArray * array = [[NSMutableArray alloc] init];
    if ([_dataBase open]) {
        NSString * sql = @"select * from appdetail";
        //查询
        FMResultSet * res = [_dataBase executeQuery:sql];
        //遍历
        while ([res next]) {
            LKVideoModel * model = [[LKVideoModel alloc] init];
            model.videoId = [res stringForColumn:@"videoId"];
            model.videoName = [res stringForColumn:@"videoname"];
            model.coverImage = [res stringForColumn:@"videoportraitUri"];
            model.isRecommend = [res stringForColumn:@"videoisRecommend"];
            model.praiseCount = [res stringForColumn:@"prisenumber"];
            model.collectionCount = [res stringForColumn:@"collectnumber"];
            model.videoUrl = [res stringForColumn:@"videourl"];
            [array addObject:model];
        }
        //关闭
        [_dataBase close];
    }
    
    return array;
}
//判断是否存在
- (BOOL)APPIsExist:(LKVideoModel *)model{
    BOOL result = NO;
    
    NSString * sql = @"select * from appdetail where videoId = ?";
    //查询
    if ([_dataBase open]) {
        FMResultSet * res = [_dataBase executeQuery:sql,model.videoId];
        //遍历
        while ([res next]) {
            //如果能进入循环，就说明已经存在
            result = YES;
        }
        [_dataBase close];
    }
    
    return result;

}
//从表中获取用户信息
- (LKVideoModel *)getUserByUserId:(NSString *)userId {
    __block LKVideoModel *model = nil;
     NSString * sql = @"select * from appdetail where videoId = ?";
    if ([_dataBase open]) {
         FMResultSet * res = [_dataBase executeQuery:sql,userId];
        while ([res next]) {
            model.videoId = [res stringForColumn:@"videoId"];
            model.videoName = [res stringForColumn:@"videoname"];
            model.coverImage = [res stringForColumn:@"videoportraitUri"];
            model.isRecommend = [res stringForColumn:@"videoisRecommend"];
            model.praiseCount = [res stringForColumn:@"prisenumber"];
            model.collectionCount = [res stringForColumn:@"collectnumber"];
            model.videoUrl = [res stringForColumn:@"videourl"];
        }
        [_dataBase close];
    }
    return model;
}
- (BOOL)deleteByUserID:(NSString *)userId {
    BOOL success;
    NSString * sql = @"delete from appdetail where videoId = ?";
    if ([_dataBase open]) {
        if ([_dataBase executeUpdate:sql,userId]) {
            success = YES;
        }
        success = NO;
        [_dataBase close];
    }
    
    return success;
}



@end
